var common = require('../../common.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    rid:0,
    linkman:'',
    phone:'',
    checkInTime:'09:30',
    mem:'',
    date:[],
    price:[],
    time: ['08:00', '08:30', '09:00', '09:30', '10:00', '10:30', '11:00', '11:30', '12:00', '12:30', '13:00', '13:30'
      , '14:00', '14:30', '15:00', '15:30', '16:00', '16:30', '17:00', '17:30', '18:00', '18:30'
      , '19:00', '19:30', '20:00', '20:30', '21:00', '21:30']
  },
  bindPickerChange: function (e) {
    wx.setStorageSync('checkInTime', this.data.time[e.detail.value]);
    this.setData({
      checkInTime: this.data.time[e.detail.value]
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    var date = options.date.split(',');
    date.shift();
    var price = options.price.split(',');
    price.shift();
    var totalMoney=0;
    for(var i=0;i<price.length;i++){
      totalMoney +=parseInt(price[i]);
    }

    var data={
      rid:options.rid,
      date:date,
      price:price,
      totalMoney: totalMoney
    }

    data.mem = wx.getStorageSync('mem');
    data.linkman = wx.getStorageSync('linkman');
    data.phone = wx.getStorageSync('phone');
    const checktime = wx.getStorageSync('checkInTime');
    if(checktime){
      data.checkInTime = checktime;
    }
    
    
    this.setData(data);
  
  },
  inputField:function(e){    
    wx.setStorageSync(e.currentTarget.dataset.field,e.detail.value);
  },
  delDate:function(e){
    console.log(e)
    for(var i=0;i<this.data.date.length;i++){
      if (this.data.date[i] == e.target.dataset.date){
        this.data.date.splice(i,1);
        this.data.totalMoney -= this.data.price[i];
        this.data.price.splice(i, 1);
        break;
      }
    }
    this.setData(
      {
        totalMoney: this.data.totalMoney,
        date:this.data.date
      }
    )
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  submitOrder: function (e) {
    var that = this;

    var data = {
      rid: that.data.rid,
      checkInTime:that.data.checkInTime,
      selectedDate: that.data.date,
      totalMoney:that.data.totalMoney,
      linkman: e.detail.value.linkman,
      phone: e.detail.value.phone,
      mem: e.detail.value.mem
    }
    common.request(
      {
        url: 'c=Game&a=buildOrder',
        data: data,
        success: function (re) {
          console.log(re);
          if (re.status == 0) {
            that.pay(that.data.totalMoney, re.oid.toString());
          } else {
            wx.showModal({
              title: '出现错误',
              content: re.msg,
            })
          }
        },
        fail: function (re) { },
        complete: function (re) { }
      }
    );

    //
  },
  pay: function (fee, gid) {//fee单位：元
    var payInfo = {
      body: '支付房费',
      total_fee: fee,
      order_sn: gid
    }
    this.basePay(payInfo, function (e) {
      common.request({
        url: 'm=Api&c=Api&a=checkPayStatus',
        data: { 'id': gid, 'type': 'g' },
        success: function (_payResult) {

        }
      })


    }, function (e) {



    });
  },

  /** 
 * 支付函数 
 * @param  {[type]} _payInfo [description] 
 * @return {[type]}          [description] 
 */
  basePay: function (_payInfo, success, fail) {
    var payInfo = {
      body: '',
      total_fee: 0,
      order_sn: ''
    }
    Object.assign(payInfo, _payInfo);
    if (payInfo.body.length == 0) {
      wx.showToast({
        title: '支付信息描述错误'
      })
      return false;
    }
    if (payInfo.total_fee == 0) {
      wx.showToast({
        title: '支付金额不能0'
      })
      return false;
    }
    if (payInfo.order_sn.length == 0) {
      wx.showToast({
        title: '订单号不能为空'
      })
      return false;
    }
    var This = this;

    payInfo.openid = app.globalData.openid;
    common.request({
      url: 'c=Pay&a=prepay',
      data: payInfo,
      success: function (res) {
        if (!res.status) {
          wx.showToast({
            title: res['errmsg']
          })
          return false;
        }
        var data = res.data;
        console.clear()
        console.log(data);

        common.request({
          url: 'c=Pay&a=pay',
          data: { prepay_id: data.data.prepay_id },
          success: function (_payResult) {
            var payResult = _payResult;
            console.log(payResult);
            wx.requestPayment({
              'timeStamp': payResult.timeStamp.toString(),
              'nonceStr': payResult.nonceStr,
              'package': payResult.package,
              'signType': payResult.signType,
              'paySign': payResult.paySign,
              'success': function (succ) {
                success && success(succ);
              },
              'fail': function (err) {
                fail && fail(err);
              },
              'complete': function (comp) {

              }
            })
          }
        })
      }
    })

  }  
})